<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('首页快捷入口') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('组件地址')" prop="component">
        <el-input
          v-model="queryParams.component"
          :placeholder="$t('组件地址')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('分类')" prop="group">
        <el-select v-model="queryParams.group" :placeholder="$t('请选择')" clearable>
          <el-option
            v-for="dict in groupOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发布场景')" prop="scene">
        <dictSelect v-model="queryParams.scene" dictionary="scene" :placeholder="$t('发布场景')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:quick:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['system:quick:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="homeQuickList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('名称')" align="center" prop="name">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.name) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发布场景')" align="center" prop="scene" :formatter="formatScene" />
      <el-table-column :label="$t('组件地址')" align="center" prop="component" />
      <el-table-column :label="$t('分类')" align="center" prop="group" :formatter="groupFormat" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:quick:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >
            {{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['system:quick:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页快捷入库对话框 -->
    <el-dialog :title="$t('首页快捷入口')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false">
      <homeQuickForm ref="homeQuickForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listHomeQuick, delHomeQuick, exportHomeQuick } from '@/api/system/home/homeQuick'
import homeQuickForm from './homeQuickForm.vue'
export default {
    components: {
        homeQuickForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 首页快捷入库表格数据
          homeQuickList: [],
          // 分类字典数据
          groupOptions: [],
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              logo: undefined,
              name: undefined,
              component: undefined,
              scene: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          // 发布场景字典
          sceneOptions: []
      }
  },
  created() {
      this.getList()
      this.getDicts('home_group').then(response => {
        this.groupOptions = response.data
      })
      this.getDicts('scene').then(res => {
        this.sceneOptions = res.data
      })
  },
  methods: {
    /** 查询首页快捷入库列表 */
    getList() {
        this.loading = true
        listHomeQuick(this.queryParams).then(response => {
            this.homeQuickList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 分类字典翻译
    groupFormat(row) {
      return this.selectDictLabel(this.groupOptions, row.group)
    },
    // 场景字典翻译
    formatScene(row) {
      return this.selectDictLabel(this.sceneOptions, row.scene)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            logo: undefined,
            name: undefined,
            component: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.homeQuickForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.homeQuickForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delHomeQuick(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportHomeQuick(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
